package com.introse.student;

import java.sql.Date;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;

/**Represents the time range for a class. The start time should be the Calendar object containing entries for the
 * Calendar.HOUR_OF_DAY and Calendar.MINUTE fields.
 * 
 * @author Kevin
 *
 */
public class ClassTime {
	
	/**The Calendar object representing the starting time range**/
	private Calendar classStartTime; 
	/**The Calendar object representign the ending time range**/
	private Calendar classEndTime;
	
	/**Create a ClassTime object with the class's start time and the class's end time
	 * 
	 * @param classStartTime The start time
	 * @param classEndTime The end time
	 */
	public ClassTime(Calendar classStartTime, Calendar classEndTime) {
		this.classStartTime = classStartTime;
		this.classEndTime = classEndTime;
	}
	
	/**Create a ClassTime object with the class's start hour and minute and the class's end hour and minute
	 * 
	 * @param classStartHour The hour the class starts
	 * @param classStartMinute The minute the class starts
	 * @param classEndHour The hour the class ends
	 * @param classEndMinute The minute the class ends
	 */
	public ClassTime(int classStartHour, int classStartMinute, int classEndHour, int classEndMinute) {
		this.classStartTime = Calendar.getInstance();
		classStartTime.clear();
		this.classStartTime.set(Calendar.HOUR_OF_DAY, classStartHour);
		this.classStartTime.set(Calendar.MINUTE, classStartMinute);
		this.classStartTime.set(Calendar.SECOND, 0);
		this.classEndTime = Calendar.getInstance();
		classEndTime.clear();
		this.classEndTime.set(Calendar.HOUR_OF_DAY, classEndHour);
		this.classEndTime.set(Calendar.MINUTE, classEndMinute);
		this.classEndTime.set(Calendar.SECOND, 0);
	}
	
	/**Compares two class times by computing the difference (by milliseconds, based on the Epoch) of two ClassTime objects.
	 * More specifically, it yields the time difference between classTime2's starting time and classTime1's ending time.
	 * 
	 * @param classTime1 The class time that will serve as the time before classTime2
	 * @param classTime2 The class time that goes after classTime1
	 * @return The difference between classTime2 and classTime1 (classTime2 Start - classTime1 End)
	 */
	public static long getTimeDifference(ClassTime classTime1, ClassTime classTime2) {
		return getTimeDifference(classTime2.classStartTime.getTimeInMillis(), classTime1.classEndTime.getTimeInMillis());
	}
	
	/**Compares two class times by computing the difference of classTime1's end time and classTime2's start time,
	 * and returns a String representation of it.
	 * 
	 * @param classTime1 The class time that will serve as the time before classTime2
	 * @param classTime2 The class time that will serve as the time after classTime1
	 * @return The string representation of the difference of classTime2 and classTime1 (classTime2 Start - classTime1 End)
	 */
	public static String getTimeDifferenceString(ClassTime classTime1, ClassTime classTime2) {
		return getTimeDifferenceString(classTime1, classTime2);
	}
	
	public static String getTimeDifferenceString (long subtrahend, long minuend) {
		SimpleDateFormat format = new SimpleDateFormat("hh:mm");	
		return format.format(new Date(getTimeDifference(subtrahend,minuend)));
	}
	
	public static long getTimeDifference(long subtrahend, long minuend) {
		return minuend - subtrahend;
	}
	
	public static ClassTime getTimeRange(ClassTime time1, ClassTime time2) {
		
		Calendar start = time1.getEndTime();
		start.add(Calendar.MINUTE, 1);
		Calendar end = time2.getStartTime();
		end.add(Calendar.MINUTE, -1);
		
		return new ClassTime (start, end);
	}
	
	/**Checks for conflict in time from the pre-existing list of times when adding a new time
	 * 
	 * @param classStartTime The new class' start time
	 * @param classEndTime The new class' end time
	 * @return
	 */
	public static boolean hasConflict(Calendar classStartTime, Calendar classEndTime, ArrayList<ClassTime> times) {
		for (ClassTime time : times) {
			if (time.getStartTime().getTimeInMillis() == classStartTime.getTimeInMillis()) {
				/**if the start time of a pre-existing class is the same as the start time of a new class, then there is a conflict**/
				return true;
			}
			else if (time.getStartTime().getTimeInMillis() < classStartTime.getTimeInMillis() && classStartTime.getTimeInMillis() < time.getEndTime().getTimeInMillis()) {
				/**The start time of a class is within another class's start time and end time**/
				return true;
			}
			else if (classEndTime.getTimeInMillis() > time.getStartTime().getTimeInMillis() && classEndTime.getTimeInMillis() < time.getEndTime().getTimeInMillis()) {
				/**The end time of a class is within another class's start time and end time**/
				return true;
			}
			
		}
		return false;
	}
	
	/**Get the start time of a CLassTime object**/
	public Calendar getStartTime () {
		return classStartTime;
	}
	
	
	/**Get the end time of a ClassTime object**/
	public Calendar getEndTime () {
		return classEndTime;
	}
	
	
}
